package com.ego.framework.interceptor;

import com.ego.common.utils.JwtUtil;
import com.ego.framework.utils.UserHolder;
import com.ego.system.domain.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义拦截器
 */
@Component
public class TokenInterceptor implements HandlerInterceptor {
    /**
     * 前置处理
     */

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1、获取头信息
        String token = request.getHeader("token");

        //2、调用service根据token查询用户
        Integer num = JwtUtil.verify(token);
        if (num != 0) {
            User user = new User();
            user.setId(num);
            //4、将对象存入Threadlocal
            UserHolder.set(user);
            return true;
        }
        return false;
    }
}